//
//  UserinfoHeadView.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/8.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

class UserinfoHeadView: UIView {

    var userBGImageView:UIImageView!
    private var useHeadImageView:UIImageView!
    
    private var userNameLabel:UILabel!
    private var onlineTimeLabel:UILabel!
    private var introLabel:UILabel!
    
    private var activityView: UIActivityIndicatorView!
    
    var userHeadAction:(()->())?
    
    
    var user:User? {
        
        didSet {
            
            self.setData()
        }
    }
    
    
    var viewHeight:CGFloat {
        
        get {
            
            return calcViewHeight(user?.label ?? "说点什么")
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        self.backgroundColor = UIColor.white

//        背景图片
        userBGImageView = UIImageView(image: #imageLiteral(resourceName: "placeholder"))
        userBGImageView.contentMode = .scaleAspectFill
        userBGImageView.clipsToBounds = true
        userBGImageView.frame = CGRect(x: 0, y: 0, width: CGFloat(kScreenWidth), height: CGFloat(kScreenWidth) * 9 / 16)
        
        self.addSubview(userBGImageView)
        
        let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
        
        userBGImageView.addSubview(effectView)
        
        effectView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
        
        
//        userBGImageView.snp.makeConstraints { maker in
//
//            maker.left.equalTo(self.snp.left)
//            maker.right.equalTo(self.snp.right)
//            maker.top.equalTo(self.snp.top)
//
//            maker.height.equalTo(CGFloat(kScreenWidth) * 9 / 16)
//        }
        
//        头像
        useHeadImageView = UIImageView(image: #imageLiteral(resourceName: "default"))
        useHeadImageView.contentMode = .scaleAspectFill
        useHeadImageView.clipsToBounds = true
        
        useHeadImageView.layer.borderWidth = 0.5
        useHeadImageView.layer.borderColor = UIColor.white.cgColor
        useHeadImageView.layer.cornerRadius = 45
        
        useHeadImageView.isUserInteractionEnabled = true
        
        useHeadImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(userHeadAction(_:))))
        
        self.addSubview(useHeadImageView)
        
        useHeadImageView.snp.makeConstraints { maker in
            
            maker.left.equalTo(userBGImageView.snp.left).offset(16)
            maker.top.equalTo(userBGImageView.snp.bottom).offset(-45)
            maker.width.height.equalTo(90)
        }
        
//        昵称
        
        userNameLabel = UILabel()
        userNameLabel.sizeToFit()
        userNameLabel.textColor = UIColor.white
        userNameLabel.font = kFont14
        
        self.addSubview(userNameLabel)
        
        userNameLabel.snp.makeConstraints { maker in
            
            maker.top.equalTo(useHeadImageView.snp.top).offset(10)
            maker.left.equalTo(useHeadImageView.snp.right).offset(16)
        }
        
        onlineTimeLabel = UILabel()
        onlineTimeLabel.sizeToFit()
        onlineTimeLabel.textColor = UIColor.black.withAlphaComponent(0.5)
        onlineTimeLabel.font = kFont12
        
        self.addSubview(onlineTimeLabel)
        
        onlineTimeLabel.snp.makeConstraints { maker in
            
            maker.bottom.equalTo(useHeadImageView.snp.bottom).offset(-10)
            maker.left.equalTo(userNameLabel.snp.left)
        }
        
//        简介
        
        introLabel = UILabel()
        introLabel.numberOfLines = 0
        introLabel.sizeToFit()
        introLabel.textColor = UIColor.black
        introLabel.font = kFont12
        
        self.addSubview(introLabel)
        
        introLabel.snp.makeConstraints {
            
            [weak self] maker in
            guard let `self` = self else { return }
            
            maker.top.equalTo(useHeadImageView.snp.bottom).offset(8)
            maker.left.equalTo(useHeadImageView.snp.left)
            maker.right.equalTo(self.snp.right).offset(-16)
        }
        
        let linView = UIView()
        
        linView.backgroundColor = UIColor.groupTableViewBackground
        
        self.addSubview(linView)
        
        linView.snp.makeConstraints {
            
            [weak self] maker in
            guard let `self` = self else { return }
            
            maker.left.equalTo(self.snp.left)
            maker.right.equalTo(self.snp.right)
            maker.top.equalTo(introLabel.snp.bottom).offset(16)
            maker.bottom.equalTo(self.snp.bottom).offset(1)
        }
        
        activityView = UIActivityIndicatorView(activityIndicatorStyle: .white)
        activityView.hidesWhenStopped = false
        activityView.center = CGPoint(x: CGFloat(kScreenWidth) / 2.0, y: -20)
        
        self.addSubview(activityView)
        
        
        var frame = self.frame
        
        frame.size.height = self.viewHeight
        
        self.frame = frame
    }
    
    func starLoadingAnimation() {
    
        activityView.startAnimating()
        
//        UIView.animate(withDuration: 0.25) {
//
//            self.activityView.center = CGPoint(x: CGFloat(kScreenWidth) / 2.0, y: CGFloat(kNavigationBarHeight))
//        }
        
    }
    
    func stopLoadingAnimation() {
        
        
        activityView.stopAnimating()
//        UIView.animate(withDuration: 0.25, animations: {
//            self.activityView.center = CGPoint(x: CGFloat(kScreenWidth) / 2.0, y: 0)
//
//        }) { (_) in
//
//            self.activityView.stopAnimating()
//        }
    }
    
    private func setData() {
        
        guard let user = self.user else { return }
        
        userBGImageView.setImage(string: user.logo_new?.getQiniuThumb(thumbImageWidth), placeholder: #imageLiteral(resourceName: "placeholder"))
        useHeadImageView.setImage(string: user.logo_new?.getQiniuThumb(thumbImageWidth), placeholder: #imageLiteral(resourceName: "default"))
        userNameLabel.text = user.nickname
        introLabel.text = user.label
        
        let time = user.updated_at?.leftTimeStringWithFormat("MM-dd HH:mm") ?? "未知"
        onlineTimeLabel.text = "最后活动于：\(time)"
    }
    
    private func calcViewHeight(_ intro:String) -> CGFloat {
        
        let bgH = CGFloat(kScreenWidth) * 9 / 16
        
        let textHeight = intro.getHeight(font: kFont12!, width: CGFloat(kScreenWidth - 32))
        
        
        let height = bgH + 45 + 8 + 16 + textHeight + 8
        
        return height
        
    }
    
    @objc private func userHeadAction(_ gesture:UITapGestureRecognizer) {
        
        self.userHeadAction?()
    }

}
